package service;

import JDBC.DBUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import method.AccountBuyInformation;
import method.AccountReleaseInformation;
import method.ItemInformation;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Gao Shiwei
 * this class will show all order information including transportation state
 */
@WebServlet(name = "OrderInformation")
public class OrderInformation extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
        resp.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, If-Modified-Since");
        resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        resp.setHeader("Access-Control-Max-Age", "3600");
        resp.addHeader("Access-Control-Allow-Credentials", "true");
        HttpSession session=req.getSession();
        String PhoneNumber=(String)session.getAttribute("PhoneNumber");

        JsonObject Container=new JsonObject();

        com.google.gson.JsonArray array=new JsonArray();

        JsonObject oneItem=new JsonObject();
        AccountBuyInformation[] Release= DBUtils.QueryAccountBuyInformation(PhoneNumber);
        for(int i=0;i<Release.length;i++){
            oneItem.addProperty("ItemID", Release[i].getItemID());
            //oneItem.addProperty("ItemQuantity",Release[i].getItemQuantity());
            ItemInformation[] Item=DBUtils.QueryItemInformation("ItemID",Release[i].getItemID());
            oneItem.addProperty("ItemName",Item[0].getItemName());
            oneItem.addProperty("ItemPrice",Item[0].getItemPrice());
            oneItem.addProperty("ItemType",Item[0].getItemType());
            oneItem.addProperty("ItemQuantity",Release[i].getItemQuantity());
            oneItem.addProperty("ItemDescription",Item[0].getItemDescription());
            Timestamp time=Release[0].getCreateTime();
            long mill=time.getTime();
            Date date = new Date();
            date.setTime(mill);
            System.out.println(date);
            oneItem.addProperty("PurchaseTime",mill);
            //AccountBuyInformation[] BuyInformation= DBUtils.QueryAccountBuyOneInformation(ItemID,PhoneNumber,Mill);
            //Timestamp time=BuyInformation[0].getCreateTime();
            //long mill=time.getTime();
            Timestamp d = new Timestamp(System.currentTimeMillis());
            long current=d.getTime();
            System.out.println(d);
            long hour=(current-mill)/60/1000;
            if(hour<=10){

                oneItem.addProperty("TransportationState","The shopkeeper hasn't delivered yet");
            }else if(hour>10&&hour<=20){
                oneItem.addProperty("TransportationState","The goods are in transit");

            }else{
                oneItem.addProperty("TransportationState","Goods arrived");

            }
            String picture=DBUtils.QueryItemPicture(Release[i].getItemID());
            if(picture!=null)
                picture.replace("\\","\\\\");
            oneItem.addProperty("Picture",picture);
            array.add(oneItem);
            oneItem=new JsonObject();
        }
        Container.add("Item",array);
        PrintWriter out = resp.getWriter();
        out.println(Container);
        out.flush();
        out.close();
    }
}
